app.controller("goodsSpecificationController",function ($scope,$controller,goodsSpecificationService) {
    $controller("commonController",{$scope:$scope});
    //分页查询
    $scope.search=function (pageNum,pageSize) {
        goodsSpecificationService.search(pageNum,pageSize).success(
            function (response) {
                $scope.list=response.result;
                $scope.paginationConf.totalItems=response.total
        });
    }
    //查询实体
    $scope.findOne=function(id){
        goodsSpecificationService.findOne(id).success(
            function(response){
                $scope.entity= response;
            }
        );
    }
    //保存
    $scope.save=function(){
        var serviceObject;//服务层对象
        if($scope.entity.dpSpecification.id!=null){//如果有ID
            serviceObject=goodsSpecificationService.update( $scope.entity ); //修改
        }else{
            serviceObject=goodsSpecificationService.add( $scope.entity  );//增加
        }
        serviceObject.success(
            function(response){
                if(200==response.code){
                    //重新查询
                    $scope.reloadList();//重新加载
                }else{
                    alert(response.message);
                }
            }
        );
    }

    $scope.entity={dpSpecification:{},dpSpecificationOptList:[]};
    //新增行
    $scope.addTableRow=function(){
        $scope.entity.dpSpecificationOptList.push({});
    }
    //删除行及规格选项
    $scope.deleteTableRow=function (index,id) {
        $scope.entity.dpSpecificationOptList.splice(index,1);
        if (id != null){
            goodsSpecificationService.deleteOpt(id).success(
                function (response) {
                    if (200==response.code){
                        alert("删除规格选项成功");
                    }else{
                        alert("删除规格选项失败");
                    }
                });
        }
    }
    //批量删除
    $scope.delete=function(){
        //获取选中的复选框
        goodsSpecificationService.delete( $scope.selected ).success(
            function(response){
                if(200 == response.code){
                    $scope.reloadList();//刷新列表
                    $scope.selected=[];
                }
            }
        );
    }

});